﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataAccess;
namespace SQT.tracnghiem.BaiViet
{
    public partial class ReadingNews : System.Web.UI.Page
    {
        public int id, count_tags = 0;
        public string[] tags = new string[10];
        public int[] relate = new int[10];
        public int count = 0;
        public string title, description, image_url;
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
            id = int.Parse(RouteData.Values["id"].ToString());
            var get_news = new TblNewsController().FetchAll().Where(x => x.NewsId == id).Single();
            title = get_news.NewsTitle;
            description = get_news.NewsReview;
            image_url = get_news.NewsImages;
            if (get_news.NewsTags != "")
            {
                if (get_news.NewsTags.IndexOf(',') != -1)
                {
                    string[] split = new string[get_news.NewsTags.Split(',').Length];
                    split = get_news.NewsTags.Split(',');
                    for (int i = 0; i < get_news.NewsTags.Split(',').Length; i++)
                    {
                        tags[i] = split[i];
                        count_tags++;
                    }
                }
                else
                {
                    count_tags = 1;
                    tags[0] = get_news.NewsTags;
                }

            }
            var get_view = new TblNewsController().FetchAll().Where(x => x.NewsId == id).Single();

            new TblNewsController().Update(id, null, null, null, null, null, null, null, null, null, null, null, get_view.NewsViewed + 1, null, null, null);

            for (int i = 0; i < count_tags; i++)
            {
                var get_news_all = new TblNewsController().FetchAll().Where(x => x.NewsDelete == false && x.NewsActive == true && x.NewsId != id);

                foreach (TblNews news in get_news_all)
                {
                    if (count > 9)
                        break;
                    if (check_relate_news(int.Parse(tags[i]), news.NewsId) == true)
                    {
                        relate[count] = news.NewsId;
                        count++;
                    }
                }

                //var get_relate = new TblNewsController().FetchAll().Where(x => x.NewsTags.IndexOf(tags[i]) != -1 && x.NewsId != id).ToList();
                //int temp = get_relate.Count;
                //if (get_relate.Count == 0)
                //    continue;
                //else
                //{
                //    int z = 0;
                //    for (int j = count; j < temp + count; j++)
                //    {
                //        relate[j] = get_relate[z].NewsId;
                //        z++;
                //    }
                //    count += get_relate.Count;
                //}
            }

            if (count < 9)
            {
                int temp = 9 - count;
                var get_relate = (from p in new TblNewsController().FetchAll().Where(x => x.NewsId != id && x.NewsActive == true && x.NewsDelete == false && x.NewsCategory == get_news.NewsCategory) orderby p.NewsDate descending select p).ToList();
                int z = 0;
                do
                {
                    if (z == get_relate.Count)
                        break;
                    if (check_relate(get_relate[z].NewsId, count, relate) == false)
                    {
                        relate[count] = get_relate[z].NewsId;
                        count++;
                    }
                    z++;
                } while (count < 9);
            }
            }
            catch
            {
                Response.Redirect("~/trang-chu");
            }
        }

        static bool check_relate_news(int tag, int id)
        {
            var get_news = new TblNewsController().FetchAll().Where(x => x.NewsDelete == false && x.NewsActive == true && x.NewsId == id).Single();

            if (get_news.NewsTags != "")
            {
                if (get_news.NewsTags.IndexOf(',') == -1)
                {
                    if (tag == int.Parse(get_news.NewsTags))
                        return true;
                }
                else
                {
                    string[] split = new string[get_news.NewsTags.Split(',').Length];
                    split = get_news.NewsTags.Split(',');

                    for (int i = 0; i < get_news.NewsTags.Split(',').Length; i++)
                    {
                        if (int.Parse(split[i]) == tag)
                            return true;
                    }
                }
            }
            return false;
        }
        static bool check_relate(int id, int count, int[] array)
        {
            for (int j = 0; j < count; j++)
            {
                if (array[j] == id)
                {
                    return true;
                }
            }
            return false;
        }
        public static string convertToUnSign(string s)
        {
            string convert = "ĂÂÀẰẦÁẮẤẢẲẨÃẴẪẠẶẬỄẼỂẺÉÊÈỀẾẸỆÔÒỒƠỜÓỐỚỎỔỞÕỖỠỌỘỢƯÚÙỨỪỦỬŨỮỤỰÌÍỈĨỊỲÝỶỸỴĐăâàằầáắấảẳẩãẵẫạặậễẽểẻéêèềếẹệôòồơờóốớỏổởõỗỡọộợưúùứừủửũữụựìíỉĩịỳýỷỹỵđ\".:;'<>?~!@#$%^&*()_+-=\\/*,";
            string To = "aaaaaaaaaaaaaaaaaeeeeeeeeeeeooooooooooooooooouuuuuuuuuuuiiiiiyyyyydaaaaaaaaaaaaaaaaaeeeeeeeeeeeooooooooooooooooouuuuuuuuuuuiiiiiyyyyyd                           ";
            for (int i = 0; i < To.Length; i++)
            {
                s = s.Replace(convert[i], To[i]);
            }
            return s;
        }
        public static string convertToUrl(string s)
        {
            string url = "";
            s = s.ToLower();
            if (s.IndexOf(' ') != -1)
            {
                string[] split = new string[s.Split(' ').Length];
                split = s.Split(' ');

                for (int i = 0; i < split.Length; i++)
                {
                    url += convertToUnSign(split[i].Trim());
                    if (i < split.Length - 1)
                        url += "-";
                }
                s = url;
            }
            else
            {
                url += convertToUnSign(s);
            }
            s = url;
            return s;
        }
        public static string limitTitle(string s)
        {
            string[] split = new string[s.Split(' ').Length];
            split = s.Split(' ');
            string title = "";
            if (split.Length > 15)
            {
                for (int i = 0; i < 15; i++)
                {
                    title += split[i];
                    title += " ";
                }
                s = title.Trim();
            }
            return s;
        }
    }
}